//
//  DHHomeController.m
//  宏达APP
//
//  Created by 123 on 2018/4/26.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHHomeController.h"
#import "DHOfficialController.h"
#import "DHNetworkManager+DHHodaNetworkManager.h"
#import "DHHomeListViewModel.h"
#import "DHHomeCell.h"

#define kScreenWidth UIScreen.mainScreen.bounds.size.width
#define kScreenHeight UIScreen.mainScreen.bounds.size.height

NSString *homeCellID = @"homeCellID";
@interface DHHomeController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *navbar;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic ,strong)DHHomeListViewModel *listViewModel;
@end

@implementation DHHomeController

// 懒加载视图模型
- (DHHomeListViewModel *)listViewModel{
    if (!_listViewModel) {
        _listViewModel = [[DHHomeListViewModel alloc] init];
    }
    return _listViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //隐藏navBar
    [self.navigationController.navigationBar setHidden:YES];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [DHNetworkManager.shareManager login];
    
    [self loadData];
    
    //弹出选择控制器
//    __weak typeof(self) WeakSelf = self;
//    self.homeView.SelectedController = ^(NSString *clsName) {
//
//        UIViewController *vc = [[NSClassFromString(clsName) alloc] init];
//
//
//          UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//
//        [WeakSelf presentViewController:nav animated:true completion:nil];
//        vc.title = @"商品列表";
//            vc.tabBarController.tabBar.hidden = YES];
//
//    };
    
    // 注册可重用cell
    //[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellid"];
        [self.tableView registerClass:[DHHomeCell class] forCellReuseIdentifier:homeCellID];
  
  
}
#pragma mark: - 加载数据
- (void)loadData{
    
    [self.listViewModel loadHomeGoodListWithFinishedBlock:^(BOOL isSuccess) {
        
        if (!isSuccess) {
            return ;
        }
        
        [self.tableView reloadData];
    }];
}

#pragma mark: - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listViewModel.homeList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DHHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:homeCellID forIndexPath:indexPath];
    
    //[self.listViewModel setupCellDataWithDict:self.listViewModel.homeList[indexPath.row]];
    
    while ([cell.contentView.subviews lastObject] != nil) {
        [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
    }
    __weak typeof(self) WeakSelf = self;
    cell.selectedButtonClick = ^(NSString *clsName) {
        
        UIViewController *vc = [[NSClassFromString(clsName) alloc] init];
        
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        
        [WeakSelf presentViewController:nav animated:true completion:nil];
        vc.title = @"商品列表";
        vc.tabBarController.tabBar.hidden = YES;
    };
    
    if ([[self.listViewModel.homeList[indexPath.row] allKeys].firstObject isEqualToString:@"goods"]) {
        
        [cell layoutIfNeeded];
    }
    cell.dictData = self.listViewModel.homeList[indexPath.row];
    
    return cell;
}

#pragma mark: - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *dict = self.listViewModel.homeList[indexPath.row];

    return [self.listViewModel calcRowHeightWithDict:dict idx:indexPath.row];

}





@end
